<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HAR to JMX Converter</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            font-weight: 300;
        }

        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        .content {
            padding: 40px;
        }

        .section {
            margin-bottom: 30px;
        }

        .section-title {
            font-size: 1.3rem;
            color: #333;
            margin-bottom: 15px;
            font-weight: 600;
        }

        .file-upload-area {
            border: 2px dashed #667eea;
            border-radius: 10px;
            padding: 40px;
            text-align: center;
            background: #f8f9ff;
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .file-upload-area:hover {
            border-color: #764ba2;
            background: #f0f2ff;
        }

        .file-upload-area.dragover {
            border-color: #764ba2;
            background: #e8ecff;
            transform: scale(1.02);
        }

        .upload-icon {
            font-size: 3rem;
            color: #667eea;
            margin-bottom: 15px;
        }

        .upload-text {
            font-size: 1.1rem;
            color: #666;
            margin-bottom: 15px;
        }

        .file-input {
            display: none;
        }

        .file-name {
            margin-top: 15px;
            padding: 10px;
            background: #e8f5e8;
            border-radius: 5px;
            color: #2d5a2d;
            font-weight: 500;
            display: none;
        }

        .convert-button {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            font-size: 1.1rem;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
            font-weight: 600;
        }

        .convert-button:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
        }

        .convert-button:disabled {
            background: #ccc;
            cursor: not-allowed;
            box-shadow: none;
        }

        .output-area {
            background: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 8px;
            padding: 0;
            overflow: hidden;
        }

        .output-header {
            background: #343a40;
            color: white;
            padding: 15px 20px;
            font-weight: 600;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .copy-button {
            background: #28a745;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 0.9rem;
            transition: background 0.3s ease;
        }

        .copy-button:hover {
            background: #218838;
        }

        .jmx-textarea {
            width: 100%;
            height: 400px;
            border: none;
            padding: 20px;
            font-family: 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.5;
            resize: vertical;
            background: white;
            color: #333;
        }

        .jmx-textarea:focus {
            outline: none;
        }

        .status-message {
            padding: 15px;
            margin: 15px 0;
            border-radius: 8px;
            display: none;
        }

        .status-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .status-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .progress-bar {
            width: 100%;
            height: 4px;
            background: #e9ecef;
            border-radius: 2px;
            overflow: hidden;
            margin: 15px 0;
            display: none;
        }

        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #667eea, #764ba2);
            width: 0;
            transition: width 0.3s ease;
        }

        @media (max-width: 768px) {
            .content {
                padding: 20px;
            }

            .header h1 {
                font-size: 2rem;
            }

            .jmx-textarea {
                height: 300px;
                font-size: 12px;
            }

            .file-upload-area {
                padding: 30px 20px;
            }
        }

        .footer {
            text-align: center;
            padding: 20px;
            color: #666;
            border-top: 1px solid #eee;
            background: #f8f9fa;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="header">
            <h1>HAR to JMX Converter</h1>
            <p>Convert HTTP Archive files to JMeter test plans effortlessly</p>
        </div>
        
        <div class="content">
            <div class="section">
                <h2 class="section-title">📁 Select HAR File</h2>
                <div class="file-upload-area" onclick="document.getElementById('harFile').click()">
                    <div class="upload-icon">📄</div>
                    <div class="upload-text">Click to select a HAR file or drag and drop</div>
                    <div class="upload-text" style="font-size: 0.9rem; opacity: 0.7;">Supported format: .har</div>
                    <input id="harFile" type="file" accept=".har" class="file-input">
                    <div id="fileName" class="file-name"></div>
                </div>
            </div>

            <div class="section">
                <div id="statusMessage" class="status-message"></div>
                <div id="progressBar" class="progress-bar">
                    <div id="progressFill" class="progress-fill"></div>
                </div>
                <button id="button" class="convert-button" disabled>
                    🔄 Convert HAR to JMX File
                </button>
            </div>

            <div class="section">
                <h2 class="section-title">📋 JMX Output</h2>
                <div class="output-area">
                    <div class="output-header">
                        <span>Generated JMX Content</span>
                        <button id="copyButton" class="copy-button" onclick="copyToClipboard()">📋 Copy</button>
                    </div>
                    <textarea id="jmxArea" class="jmx-textarea" placeholder="JMX content will appear here after conversion..."></textarea>
                </div>
            </div>
        </div>
        
        <div class="footer">
            <p>💡 This tool converts HAR files to JMeter JMX format with automatic host parameterization and global assertions.</p>
        </div>
    </div>
    <script type="text/javascript" src="/har2jmx/js/jquery-3.5.1.min.js"></script>
    <script type="text/javascript" src="/har2jmx/js/har2jmx.js"></script>
    <script>
        var harFile = document.getElementById("harFile");
        var convertButton = document.getElementById("button");
        var fileUploadArea = document.querySelector('.file-upload-area');
        var fileName = document.getElementById('fileName');
        var statusMessage = document.getElementById('statusMessage');
        var progressBar = document.getElementById('progressBar');
        var progressFill = document.getElementById('progressFill');
        var harText;
        var harFileName;

        // File drag and drop functionality
        fileUploadArea.addEventListener('dragover', function(e) {
            e.preventDefault();
            fileUploadArea.classList.add('dragover');
        });

        fileUploadArea.addEventListener('dragleave', function(e) {
            e.preventDefault();
            fileUploadArea.classList.remove('dragover');
        });

        fileUploadArea.addEventListener('drop', function(e) {
            e.preventDefault();
            fileUploadArea.classList.remove('dragover');
            
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                harFile.files = files;
                handleFileSelection(files[0]);
            }
        });

        function showStatus(message, type) {
            statusMessage.textContent = message;
            statusMessage.className = 'status-message status-' + type;
            statusMessage.style.display = 'block';
            
            if (type === 'success') {
                setTimeout(() => {
                    statusMessage.style.display = 'none';
                }, 3000);
            }
        }

        function showProgress(show) {
            progressBar.style.display = show ? 'block' : 'none';
            if (!show) {
                progressFill.style.width = '0%';
            }
        }

        function updateProgress(percent) {
            progressFill.style.width = percent + '%';
        }

        function handleFileSelection(file) {
            var dotIndex = file.name.lastIndexOf(".");
            var fileType = file.name.slice(dotIndex + 1);

            if (fileType.toLowerCase() !== "har") {
                showStatus("Please select a valid .har file", "error");
                harFile.value = "";
                fileName.style.display = 'none';
                convertButton.setAttribute('disabled', 'disabled');
                return;
            }
            
            harFileName = file.name.substring(0, dotIndex);
            fileName.textContent = '📄 Selected: ' + file.name;
            fileName.style.display = 'block';
            
            showStatus("Reading file...", "success");
            showProgress(true);
            updateProgress(25);

            var reader = new FileReader();
            reader.readAsText(file);

            reader.onload = function() {
                updateProgress(100);
                setTimeout(() => {
                    showProgress(false);
                    harText = reader.result;
                    convertButton.removeAttribute("disabled");
                    showStatus("File loaded successfully! Ready to convert.", "success");
                }, 500);
            }

            reader.onerror = function() {
                showProgress(false);
                showStatus("Error reading file. Please try again.", "error");
                convertButton.setAttribute('disabled', 'disabled');
            }
        }

        harFile.onchange = function() {
            if (harFile.files.length > 0) {
                handleFileSelection(harFile.files[0]);
            }
        }

        convertButton.onclick = function() {
            showStatus("Converting HAR to JMX...", "success");
            showProgress(true);
            updateProgress(10);
            
            // Add small delay to show progress
            setTimeout(() => {
                try {
                    updateProgress(30);
                    
                    $.ajax({
                        url: location.origin + "/har2jmx/template.jmx",
                        async: false,
                        dataType: "xml",
                        success: function(response) {
                            template = response;
                            updateProgress(60);
                        },
                        error: function() {
                            showProgress(false);
                            showStatus("Error loading template file. Please check if template.jmx exists.", "error");
                            return;
                        }
                    });

                    var har2JmxConverter = new Har2JmxConverter();
                    var jmxArea = document.getElementById("jmxArea");
                    
                    updateProgress(80);
                    jmxArea.value = har2JmxConverter.convert(template, harText);
                    
                    updateProgress(90);
                    download(harFileName + ".jmx", jmxArea.value);
                    
                    updateProgress(100);
                    setTimeout(() => {
                        showProgress(false);
                        showStatus("Conversion completed! JMX file downloaded successfully.", "success");
                    }, 500);
                    
                } catch (error) {
                    showProgress(false);
                    showStatus("Error during conversion: " + error.message, "error");
                    console.error('Conversion error:', error);
                }
            }, 100);
        }

        function download(filename, text) {
            var element = document.createElement('a');
            element.setAttribute('href', 'data:application/xml;charset=utf-8,' + encodeURIComponent(text));
            element.setAttribute('download', filename);
            element.style.display = 'none';
            document.body.appendChild(element);
            element.click();
            document.body.removeChild(element);
        }

        function copyToClipboard() {
            var jmxArea = document.getElementById('jmxArea');
            if (jmxArea.value.trim() === '') {
                showStatus('No content to copy. Please convert a HAR file first.', 'error');
                return;
            }
            
            jmxArea.select();
            jmxArea.setSelectionRange(0, 99999); // For mobile devices
            
            try {
                document.execCommand('copy');
                showStatus('Content copied to clipboard!', 'success');
            } catch (err) {
                showStatus('Failed to copy content. Please select and copy manually.', 'error');
            }
        }
        
        // Initialize copy button
        document.getElementById('copyButton').style.display = 'none';
        
        // Show copy button when there's content
        document.getElementById('jmxArea').addEventListener('input', function() {
            var copyButton = document.getElementById('copyButton');
            if (this.value.trim() !== '') {
                copyButton.style.display = 'block';
            } else {
                copyButton.style.display = 'none';
            }
        });
    </script>
</body>

</html>
